package com.ruoyi.framework.web.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.DTO.*;
import com.ruoyi.common.core.domain.entity.ComboCardConfig;
import com.ruoyi.common.core.domain.entity.ComboConfig;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.mapper.ComboCardConfigMapper;
import com.ruoyi.system.mapper.ComboConfigMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Slf4j
@Component
@Transactional
public class ComboCardConfigService {
    @Autowired
    private ComboCardConfigMapper comboCardConfigMapper;

    //批量生成卡号、卡密
    public void insertComboCardConfig(InsertComboCardConfigBO insertComboCardConfigBO) {
        //根据套餐id和批次查询是否已生成该批次的卡号和卡密
        Long count = comboCardConfigMapper.selectCountByComboIdAndBatch(insertComboCardConfigBO.getComboId(), insertComboCardConfigBO.getBatch());
        if(!ObjectUtils.isEmpty(count) && count > 0){
            throw new BusinessException("该批次的卡号和卡密已生成，请勿重复生成");
        }
        ComboCardConfig comboCardConfig = new ComboCardConfig();
        BeanUtils.copyProperties(insertComboCardConfigBO, comboCardConfig);
        for (int i = 0; i < insertComboCardConfigBO.getNumber(); i++) {
            String cardNumber = UUID.randomUUID().toString().replace("-", "").substring(0, 8).toUpperCase();
            comboCardConfig.setCardNumber(cardNumber);
            String cardKey = UUID.randomUUID().toString().replace("-", "").substring(0, 12).toUpperCase();
            comboCardConfig.setCardKey(cardKey);
            comboCardConfigMapper.insertComboCardConfig(comboCardConfig);
        }
    }

    public void updateComboCardConfig(UpdateComboCardConfigBO updateComboCardConfigBO) {
        ComboCardConfig comboCardConfig = new ComboCardConfig();
        BeanUtils.copyProperties(updateComboCardConfigBO, comboCardConfig);
        comboCardConfigMapper.updateComboCardConfig(comboCardConfig);
    }

    //分页查看卡片列表
    public PageInfo<SelectComboCardConfigVO> selectPurchaseRecord(SelectComboCardConfigBO selectComboCardConfigBO) {
        Integer pageNum = selectComboCardConfigBO.getPageNum();
        Integer pageSize = selectComboCardConfigBO.getPageSize();
        // 配置分页插件后使用
        PageHelper.startPage(pageNum, pageSize);
        List<SelectComboCardConfigVO> purchaseRecordVOS = comboCardConfigMapper.selectComboCardConfig(selectComboCardConfigBO);
        return new PageInfo<>(purchaseRecordVOS);
    }

    //导出卡片列表
    public List<SelectComboCardConfigVO> exportComboCardConfig(SelectComboCardConfigBO selectComboCardConfigBO) {
        return comboCardConfigMapper.selectComboCardConfig(selectComboCardConfigBO);
    }

    /**
     * 根据卡号和卡密查询卡片信息
     */
    public SelectComboCardConfigVO selectByCardNumberAndCardKey(String cardNumber, String cardKey) {
        return comboCardConfigMapper.selectByCardNumberAndCardKey(cardNumber, cardKey);
    }
}
